//
//  HomePageViewController.m
//  Mall
//
//  Created by 孙飞燕 on 16/9/28.
//  Copyright © 2016年 孙飞燕. All rights reserved.
//

#import "HomePageViewController.h"
#import "KitScrollView.h"
#import "HPCollectionReusableViewHeader.h"
#import "ScrollViewCollectionReusableView.h"
#import "HPDetailViewController.h"
#import "SearchResultsViewController.h"
#import "LoginViewController.h"
#import "HPFirstCollectionViewCell.h"
#import "HPSecondCollectionViewCell.h"
#import "HPThirdCollectionViewCell.h"
#import "HPFourthCollectionViewCell.h"
#define kWidth self.view.frame.size.width
#define kHeight self.view.frame.size.height
@interface HomePageViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,UISearchResultsUpdating,UISearchControllerDelegate,UISearchBarDelegate>
@property (nonatomic,strong)UISearchBar *searchBar;//搜索框
@property (nonatomic,strong)UISearchController *searchController;
@property(nonatomic, retain)NSMutableString* searchStr;

@property (nonatomic,strong)UICollectionView *collectionView;
@property (nonatomic,strong)UICollectionViewFlowLayout *flowLayout;

@property (nonatomic,strong)NSMutableArray *goodsListArr;
@property (nonatomic,strong)NSMutableArray *modelArr;//数据模型数组

@property (nonatomic,strong)NSArray *modelsArr;
@property (nonatomic,strong)NSArray *identifierArr;


@end

@implementation HomePageViewController

- (UISearchController *)searchController{
    if (!_searchController) {
        SearchResultsViewController *resultsVC = [[SearchResultsViewController alloc]init];
        _searchController = [[UISearchController alloc]initWithSearchResultsController:resultsVC];
        [self.searchController.searchBar sizeToFit];//设置searchBar位置自适应
        self.searchController.searchBar.placeholder = @"搜索商品";
        self.searchController.delegate = self; //设置显示搜索结果的控制器
        self.searchController.searchBar.delegate = self;
    }
    return _searchController;
}

- (UICollectionView *)collectionView{
    if (!_collectionView) {
        _flowLayout = [[UICollectionViewFlowLayout alloc]init];
        _collectionView = [[UICollectionView alloc]initWithFrame:self.view.frame collectionViewLayout:_flowLayout];
        _flowLayout.minimumLineSpacing = 0;
        _flowLayout.minimumInteritemSpacing = 0;
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        //注册单元格
        [_collectionView registerNib:[UINib nibWithNibName:@"HPFirstCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"HPFirstCollectionViewCell"];
        [_collectionView registerNib:[UINib nibWithNibName:@"HPSecondCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"HPSecondCollectionViewCell"];
        [_collectionView registerNib:[UINib nibWithNibName:@"HPThirdCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"HPThirdCollectionViewCell"];
        [_collectionView registerNib:[UINib nibWithNibName:@"HPFourthCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"HPFourthCollectionViewCell"];
        //注册头部视图和尾视图
        [self.collectionView registerClass:[ScrollViewCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ScrollHeader"];
        [self.collectionView registerClass:[ScrollViewCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"ScrollFooter"];
        [self.collectionView registerNib:[UINib nibWithNibName:@"HPCollectionReusableViewHeader" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HPCollectionReusableViewHeader"];
    }
    return _collectionView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.collectionView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.view addSubview:self.collectionView];
    
    //导航栏上的控件添加
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"登录" style:UIBarButtonItemStylePlain target:self action:@selector(loginAction:)];
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];//字体颜色
    self.navigationItem.titleView = self.searchController.searchBar;
    self.navigationController.navigationBar.barTintColor = [[UIColor redColor]colorWithAlphaComponent:0.6];//导航栏颜色


}
//导航栏按钮回调事件
- (void)loginAction:(UIBarButtonItem *)sender{
    //点击进入登录页面
    LoginViewController *loginVC = [[LoginViewController alloc]init];
    [self.navigationController pushViewController:loginVC animated:YES];
}
#pragma mark----UISearchController delegate
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController{
    NSString *text = searchController.searchBar.text;
    if ([text length] != 0) {
        self.searchBar = [text mutableCopy];
        SearchResultsViewController *searchResultsVC = (SearchResultsViewController *)self.searchController.searchResultsController;
        searchResultsVC.searchStr = self.searchStr;
    }
}
- (void)didDismissSearchController:(UISearchController *)searchController{

    SearchResultsViewController *searchResultsVC = (SearchResultsViewController *)self.searchController.searchResultsController;
//    [searchResultsVC.sourceArray removeAllObjects];
    self.searchStr = nil;
}
-(void)willPresentSearchController:(UISearchController *)searchController{

    self.searchStr = [NSMutableString string];
}

#pragma mark ---- UICollectionView的代理方法
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 6;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    if (section == 0) {
        return 0;
    }else if (section == 3){
        return 8;
    }else{
        return 4;
    }


}
//注册cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 1) {
        HPFirstCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HPFirstCollectionViewCell" forIndexPath:indexPath];
        return cell;
    }else if (indexPath.section == 2){
        HPSecondCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HPSecondCollectionViewCell" forIndexPath:indexPath];
        cell.cellImage.hidden = YES;
        cell.cellTitle.hidden = YES;
        cell.contentLabel.hidden = YES;
        return cell;
    }else if (indexPath.section == 3){
        HPSecondCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HPSecondCollectionViewCell" forIndexPath:indexPath];
        return cell;
    }else if (indexPath.section == 4){
        HPThirdCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HPThirdCollectionViewCell" forIndexPath:indexPath];
        return cell;

    }else if (indexPath.section == 5){
        HPFourthCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HPFourthCollectionViewCell" forIndexPath:indexPath];
        return cell;
    }else{
        return nil;
    }


}
//头视图或尾视图
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    NSMutableArray *imageArr = [NSMutableArray new];
    for (int i = 0; i < 5; i++) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"k%d.jpg",i]];
        [imageArr addObject:image];
    }
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {

       if (indexPath.section == 0) {
        //滚动视图
        ScrollViewCollectionReusableView *headView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ScrollHeader" forIndexPath:indexPath];
        UIView *scrollView = [[KitScrollView alloc]initWithFrame:headView.frame imagesArray:imageArr period:1];
        [headView addSubview:scrollView];
        return headView;
        
       }else if(indexPath.section == 3 || indexPath.section == 4 || indexPath.section == 5){
        HPCollectionReusableViewHeader *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HPCollectionReusableViewHeader" forIndexPath:indexPath];
        headerView.headerTitle.text = @"商城呼吸机";
        return headerView;
       }else{
           return nil;
       }
    }else{
        if (indexPath.section == 3 || indexPath.section == 4) {

            ScrollViewCollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"ScrollFooter" forIndexPath:indexPath];
            UIView *scrollView = [[KitScrollView alloc]initWithFrame:footerView.frame imagesArray:imageArr period:1];
            [footerView addSubview:scrollView];
            footerView.backgroundColor = [UIColor orangeColor];
            return footerView;
        }else{
            return nil;
        }


    }

}



//cell大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 1 ) {
        return CGSizeMake(kWidth/2, 120);
    }else if (indexPath.section == 2 || indexPath.section == 3){
        return CGSizeMake(kWidth/4, 150);
    }else if (indexPath.section == 4 || indexPath.section == 5){
        return CGSizeMake(kWidth/2, 150);

    }else{
        return CGSizeMake(0, 0);
    }
    
}

//头视图大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return CGSizeMake(kWidth, 200);
    }else if(section == 3 || section == 4 || section == 5){
        return CGSizeMake(kWidth, 50);
    }else{
        return CGSizeMake(0, 0);
    }
}

//尾视图大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
    if (section == 3 || section == 4) {
        return CGSizeMake(kWidth, 100);
    }else{
        return CGSizeMake(0, 0);

    }
}
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    
    return UIEdgeInsetsMake(0, 0, 0, 0);
}
//点击cell
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

    HPDetailViewController *detailVC = [[HPDetailViewController alloc]init];
    [self.navigationController pushViewController:detailVC animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
